package com.wjw.java.oop.polymorphic_;

/**
 * 多态参数：方法定义的形参类型为父类类型,实参类型允许为子类类型
 */
public class PolymorphicParameters {
    public static void main(String[] args) {
        Employee worker1 = new Worker("工人1",4500.5);
        Employee worker2 = new Worker("工人2",5500.5);
        Employee manager1 = new Manager("张经理",10000.9,30000);
        Employee manager2 = new Manager("吴经理",12000.9,30000);
        System.out.println("工人1的年收入为："+showAnnual(worker1));
        testWork(worker1);
        System.out.println("工人2的年收入为："+showAnnual(worker2));
        testWork(worker2);
        System.out.println("张经理的年收入为："+showAnnual(manager1));
        testWork(manager1);
        System.out.println("吴经理的年收入为："+showAnnual(manager2));
        testWork(manager2);
    }
    //获取任何类型员工的年度总工资
    //形参类型为父类类型,实参类型允许为子类类型
    public static double showAnnual(Employee e){
        return e.getAnnual();
    }
    //向下转型：转为Worker、Manager,然后调用自己特有的方法
    public static void testWork(Employee e){
        if(e instanceof Worker){
            Worker worker = (Worker) e;
            worker.work();
        }else if(e instanceof Manager){
            Manager manager = (Manager) e;
            manager.manage();
        }
    }
}
//父类：员工类
class Employee{
    private String name;
    private double salary;
    //获取年工资
    public double getAnnual(){
       return this.salary * 12;
    }
    //getter、setter...
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return this.salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public Employee() {
    }
    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
}
//子类：普通员工
class Worker extends Employee{
    public Worker() {
    }
    public Worker(String name, double salary) {
        super(name, salary);
    }
    //子类特有方法
    public void work(){
        System.out.println("普通员工："+super.getName()+",正在工作...");
    }
    //普通员工没有奖金,所以直接调用父类的:获取年工资方法
    @Override
    public double getAnnual() {
        return super.getAnnual();
    }
}
//子类：经理
class Manager extends Employee{
    private double bonus;//奖金
    //子类特有方法
    public void manage(){
        System.out.println("经理："+super.getName()+",正在检查工作...");
    }
    //经理的工资 = 工资 + 奖金
    @Override
    public double getAnnual() {
        return super.getAnnual()+this.bonus;
    }
    //getter、setter...
    public double getBonus() {
        return this.bonus;
    }
    public void setBonus(double bonus) {
        this.bonus = bonus;
    }
    public Manager() {
    }
    public Manager(String name, double salary, double bonus) {
        super(name, salary);
        this.bonus = bonus;
    }
}
